package tech.edwardvan.rabbitmqdemo.pattern.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;
import tech.edwardvan.rabbitmqdemo.pattern.CommonUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 路由模式-生产者
 *
 * @author EdwardVan
 */
@Slf4j
public class Producer {


    public static void main(String[] args) throws IOException, TimeoutException {
        //获取一个连接
        Connection connection = CommonUtil.getConnection();

        //从连接中获取一个通道
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(CommonUtil.EXCHANGE_ROUTING, BuiltinExchangeType.DIRECT, false, true, null);

        //需要发送的消息
        String msg = "hello routing!";

        for (int i = 0; i < 10; i++) {
            if (i % 5 == 0) {
                channel.basicPublish(CommonUtil.EXCHANGE_ROUTING, "error", null, (msg + i).getBytes());
            } else if (i % 9 == 0) {
                channel.basicPublish(CommonUtil.EXCHANGE_ROUTING, "warn", null, (msg + i).getBytes());
            } else {
                channel.basicPublish(CommonUtil.EXCHANGE_ROUTING, "info", null, (msg + i).getBytes());
            }
        }

        log.info("生产者发送消息完成");

        //关闭
        channel.close();
        connection.close();
    }
}
